package cx.xuyang.view;

import java.util.ArrayList;

import com.example.selecthistorymenu.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.TextView;


public class SelectHistoryMenu extends ViewGroup implements OnClickListener,OnLongClickListener{
	private ArrayList<String> mDatas;
	private float mDefaultMetrics=160;
	private float mNowMetrics;
	private float mMetricsProportion;
	
	private int mTxtSize;
	public SelectHistoryMenu(Context context) {
		this(context, null);
	}
	public SelectHistoryMenu(Context context, AttributeSet attrs) {
		this(context, attrs,null);
	}
	public SelectHistoryMenu(Context context, AttributeSet attrs,ArrayList<String> datas) {
		super(context, attrs);
		TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.SelectHistoryMenu);
		mTxtSize=array.getInt(R.styleable.SelectHistoryMenu_text_size, 16);
		mNowMetrics=getResources().getDisplayMetrics().xdpi;
		mMetricsProportion=mNowMetrics/mDefaultMetrics;
		mDatas=datas;
		notifyDataSetChanged();
	}
	//测量，测量素有子控件的长宽
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int padding=(int) (5*mMetricsProportion);
		int height=(int) (5*mMetricsProportion);
		int marginLeft=padding;
		int marginTop=padding;
		int widthSize=MeasureSpec.getSize(widthMeasureSpec);
		widthMeasureSpec=MeasureSpec.makeMeasureSpec(widthSize-(int)(15*mMetricsProportion), MeasureSpec.AT_MOST);
		//此步操作做的是测量所有子类view，让所有子类view测量完毕，确定大小，然后进行下一步layout布局
		for (int i = 0; i < getChildCount(); i++) {
			measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
		}
		for (int i = 0; i < getChildCount(); i++) {
			TextView view=(TextView) getChildAt(i);
			if (i==0) {
				height+=view.getMeasuredHeight()+padding;
			}
			//如果当前行剩余空间不足以放下当前项，则换行
			if (marginLeft+padding+view.getMeasuredWidth()>=widthSize&&marginLeft!=padding) {
				marginTop=marginTop+view.getMeasuredHeight()+padding;
				marginLeft=padding;
				height+=padding+view.getMeasuredHeight();
			}
			LayoutParam param=new LayoutParam(marginLeft, marginLeft+view.getMeasuredWidth(), marginTop, marginTop+view.getMeasuredHeight());
			view.setTag(param);
			marginLeft=marginLeft+view.getMeasuredWidth()+padding;
		}
		setMeasuredDimension(widthSize, height);
	}
	//布局，将所有子控件布置位置
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		for (int i = 0; i < getChildCount(); i++) {
			View view=getChildAt(i);
			LayoutParam param=(LayoutParam) view.getTag();
			view.layout(param.left, param.top, param.right, param.bottom);
		}
	}
	public void setDatas(ArrayList<String> data)
	{
		mDatas=data;
		notifyDataSetChanged();
	}
	public void addItem(String item)
	{
		mDatas.add(item);
		notifyDataSetChanged();
	}
	//当数据源发生变化时
	public void notifyDataSetChanged(){
		removeAllViews();
		if (mDatas==null) {
			return;
		}
		for (int i = mDatas.size()-1; i >=0; i--) {
			String str=mDatas.get(i);
			TextView view=new TextView(getContext());
			view.setText(str);
			view.setBackgroundResource(R.drawable.selector_menuitem_bg);
			view.setTextColor(getResources().getColor(android.R.color.white));
			view.setGravity(Gravity.CENTER);
			view.setSingleLine(true);
			view.setEllipsize(TruncateAt.END);
			view.setPadding((int)(8*mMetricsProportion), (int)(3*mMetricsProportion), (int)(8*mMetricsProportion), (int)(3*mMetricsProportion));
			view.setTextSize(mTxtSize);
			view.setOnClickListener(this);
			view.setOnLongClickListener(this);
			this.addView(view);
		}
	}
	public interface OnSelectHistoryMenuItemClickListener{
		void onItemClick(View v);
	}
	
	OnSelectHistoryMenuItemClickListener mOnSelectHistoryMenuItemClickListener;
	public void setOnSelectHistoryMenuItemClickListener(OnSelectHistoryMenuItemClickListener onSelectHistoryMenuItemClick){
		mOnSelectHistoryMenuItemClickListener=onSelectHistoryMenuItemClick;
	}
	
	@Override
	public void onClick(View v) {
		if (mOnSelectHistoryMenuItemClickListener!=null) {
			mOnSelectHistoryMenuItemClickListener.onItemClick(v);
		}
	}
	
	public interface OnSelectHistoryMenuItemLongClickListener{
		void onLongClick(View v);
	}
	OnSelectHistoryMenuItemLongClickListener mOnSelectHistoryMenuItemLongClickListener;
	public void setOnMenuItemLongClickListener(OnSelectHistoryMenuItemLongClickListener onSelectHistoryMenuItemLongClickListener){
		mOnSelectHistoryMenuItemLongClickListener=onSelectHistoryMenuItemLongClickListener;
	}
	
	public interface OnSelectHistoryMenuItemDeleteListener{
		void onDeleteClick(String v);
	}
	OnSelectHistoryMenuItemDeleteListener mOnSelectHistoryMenuItemDeleteListener;
	public void setOnMenuItemDeleteListener(OnSelectHistoryMenuItemDeleteListener onSelectHistoryMenuItemDeleteListener){
		mOnSelectHistoryMenuItemDeleteListener=onSelectHistoryMenuItemDeleteListener;
	}
	@Override
	public boolean onLongClick(final View v) {
		AlertDialog dialog=new AlertDialog.Builder(getContext())
		.setTitle("确认信息")
		.setMessage("确定删除？")
		.setPositiveButton("确认", new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				removeView(v);
				if (mOnSelectHistoryMenuItemDeleteListener!=null) {
					mOnSelectHistoryMenuItemDeleteListener.onDeleteClick(((TextView)v).getText().toString());
				}
			}
		})
		.setNeutralButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		})
		.create();
		dialog.show();
	
		if (mOnSelectHistoryMenuItemLongClickListener!=null) {
			mOnSelectHistoryMenuItemLongClickListener.onLongClick(v);
		}
		return false;
	}
	
	
	class LayoutParam{
		int left,right,top,bottom;
		public LayoutParam() {
		}
		public LayoutParam(int left, int right, int top, int bottom) {
			super();
			this.left = left;
			this.right = right;
			this.top = top;
			this.bottom = bottom;
		}
		
	}
}
